#include "EditViewParamTag.h"

#include "mcore/tools/ConstVar4MapTool.h"
#include "mcore/map/Graphic.h"
#include "IdRootMenu.h"

#include "mcore/tools/EditWidget.h"
#include "mcore/tools/setting/KeToolSettings.h"


#define RestBtnEditState()     ActDrawMode4Pick = MenuId_None;  \
VertexOrEdgeTarget4Pick  = Pick_WholeGeom;  \
    ActNewDraw = MenuId_None;  \
    ActGeomEdit = MenuId_None;   \
    IdentifyDrawMode4Load = MenuId_None;



namespace kepen {
namespace core {

EditViewParamTag::EditViewParamTag(EditWidget * ew):


    m_isEnableAffinePart(false),
    m_IsOptMakeHole(false),   
    m_enSave(false),
    ActDrawMode4Pick(MenuId_None),
    VertexOrEdgeTarget4Pick(Pick_WholeGeom),
    ActNewDraw(MenuId_None),
    ActGeomEdit(MenuId_None),
    IdentifyDraw4Load(MenuId_None),
    nVertexOrEdgePick_last(Pick_WholeGeom),
    nDrawMode4Pick_last(IdentifyPick_ByRect),
    nIdentify4Load_last(IdentifyPick_ByRect),
    m_HashGeom(0),
    m_HashGeom_run(0),
    editWdg(ew)
{

}

void EditViewParamTag::resetState()
{
    ActDrawMode4Pick = MenuId_None;
    VertexOrEdgeTarget4Pick  = Pick_WholeGeom;
    ActNewDraw = MenuId_None;
    ActGeomEdit = MenuId_None;    
    IdentifyDraw4Load = MenuId_None;

    nVertexOrEdgePick_last = Pick_WholeGeom;
}
void EditViewParamTag::setActGeomEdit(int val){
    this->ActGeomEdit = val;

    ActDrawMode4Pick =  MenuId_None;    
    VertexOrEdgeTarget4Pick =  MenuId_None;
    ActNewDraw =  MenuId_None;
    IdentifyDraw4Load = MenuId_None;
}

void EditViewParamTag::setActGeomNew(int val)
{
    this->ActNewDraw = val;

    ActDrawMode4Pick =  MenuId_None;
    VertexOrEdgeTarget4Pick =  MenuId_None;
    ActGeomEdit =  MenuId_None;
    IdentifyDraw4Load = MenuId_None;
}

bool EditViewParamTag::isEnPickPartMode()const{
    return MenuId_None == ActGeomEdit
           && MenuId_None == IdentifyDraw4Load;
}

bool EditViewParamTag::isCheckedBy(int id)const{
    bool  _isChecked = false;
    int val;
    if(Option_MakeHole == id){
        _isChecked = m_IsOptMakeHole;
    }else if(Option_VisibleVertexIndex ==id)
    {
        KeToolSettings *keSettingRR2 = editWdg->getMapToolSettings();
        _isChecked = keSettingRR2->m_visibleVertexIndex;
    }else if(Option_EnSnapGraphic ==id)
    {
        KeToolSettings *keSettingRR2 = editWdg->getMapToolSettings();
        _isChecked = keSettingRR2->m_EnSnapGraphic;
    }
    else if (EditTool_Vector == id
             || EditTool_LineSlide == id
             || EditTool_LineExtrude == id
             ||EditTool_Pan == id || EditTool_Zoom == id
             || EditTool_Rotate == id || EditTool_Shear == id)
    {
        val =  this->ActGeomEdit;
        _isChecked = (id == val);
    } else if (IS_DrawTool_By(id)) {
        val =  this->ActNewDraw;
        _isChecked = (id == val);

    }else if (Pick_Target_Vertex == id || Pick_Target_Edge == id) {
        val = this->VertexOrEdgeTarget4Pick;
        _isChecked = (id == val);

    }else if (IdentifyPick_ComboBox == id) {
        val = this->ActDrawMode4Pick;
        _isChecked = false ==(MenuId_None == val);

    }else if (IdentifyLoad_ComboBox == id){
        val = this->IdentifyDraw4Load;
        _isChecked = false ==(MenuId_None == val);
    }
    return  _isChecked;
}

bool EditViewParamTag::enableBy(int id)const{
    bool  isEnable = true;
    if(IS_DrawTool_By(id))return true;

    if (FileSave_BtnId == id) {
        isEnable = enableDoSave();
    }else if (!(Goback_BarBtnId == id) ) {
        isEnable =  isNotEmptyGeom();

    }else if (Option_MakeHole == id ) {
        isEnable =  isNotEmptyGeom();

    }/*else if (EditTool_Zoom == id
               || EditTool_Rotate == id
               || EditTool_Shear == id) {
        isEnable =  this->m_isEnableAffinePart;
          强制清空selectedPoints,因此，这里 不做判断了
    }*/else if (Pick_Target_Vertex == id ||
             Pick_Target_Edge == id ||
             IdentifyPick_ComboBox == id ) {
        isEnable =  isNotEmptyGeom() && isEnPickPartMode();

    }
    return isEnable;
}

bool  EditViewParamTag::enableDoSave() const
{
    return m_HashGeom != m_HashGeom_run;
}

bool EditViewParamTag::isNotEmptyGeom() const
{
    const  kepen::core::Graphic *editedGraphic = editWdg->getEditGraphic();
    if (editedGraphic != NULL
        && editedGraphic->getGeom() != NULL
        && editedGraphic->getGeom()->isEmpty() == false) {
        return true;
    }
    return false;
}

void EditViewParamTag::clearPickMode()
{
    VertexOrEdgeTarget4Pick = Pick_WholeGeom;
    ActDrawMode4Pick = MenuId_None;
    IdentifyDraw4Load = MenuId_None;
}




int EditViewParamTag::getComboBoxDisplay(int id) const
{
    int ret = IdentifyPick_ComboBox == id ? ActDrawMode4Pick:IdentifyDraw4Load;
    if(MenuId_None == ret){
        if(IdentifyPick_ComboBox == id) ret = nDrawMode4Pick_last;
        else if(IdentifyLoad_ComboBox == id) ret = nIdentify4Load_last;
    }

    return ret;
}

int EditViewParamTag::setTarget4Pick(int val)
{
    // ActDrawMode4Pick = MenuId_None;
    // VertexOrEdgeTarget4Pick  = Pick_WholeGeom;
    ActNewDraw = MenuId_None;
    ActGeomEdit = MenuId_None;
    IdentifyDraw4Load = MenuId_None;

    VertexOrEdgeTarget4Pick = val;
    nVertexOrEdgePick_last = val;

    if(Pick_WholeGeom == val){
        ActDrawMode4Pick = MenuId_None;
    }else if(MenuId_None == ActDrawMode4Pick){
        int n = nDrawMode4Pick_last;
        if(MenuId_None == n)
            n = IdentifyPick_ByRect;
        ActDrawMode4Pick = n;
    }


    return ActDrawMode4Pick;
}

int EditViewParamTag::setDrawMode4Pick(int val){
    // ActDrawMode4Pick = MenuId_None;
    // VertexOrEdgeTarget4Pick  = Pick_WholeGeom;

    ActNewDraw = MenuId_None;
    ActGeomEdit = MenuId_None;
    IdentifyDraw4Load = MenuId_None;

    ActDrawMode4Pick = val;
    nDrawMode4Pick_last = val;


    if(Pick_WholeGeom == val){
        VertexOrEdgeTarget4Pick = MenuId_None;
    }else if(MenuId_None == VertexOrEdgeTarget4Pick){
        int n = nVertexOrEdgePick_last;
        if(MenuId_None == n)
            n = Pick_Target_Vertex;
        VertexOrEdgeTarget4Pick = n;
    }
    return VertexOrEdgeTarget4Pick;
}

void EditViewParamTag::setIdentify4Load(int val)
{
    ActNewDraw = MenuId_None;
    ActGeomEdit = MenuId_None;
    IdentifyDraw4Load = val;
    nIdentify4Load_last = val;
    ActDrawMode4Pick = MenuId_None;

    VertexOrEdgeTarget4Pick = MenuId_None;
}


}
}
